Skip to content

feat: surface X-Logfire-Warning / X-Logfire-Error response headers#1906

Open
adriangb wants to merge 4 commits intomainfrom
add-server-response-headers
Open

feat: surface X-Logfire-Warning / X-Logfire-Error response headers#1906
adriangb wants to merge 4 commits intomainfrom
add-server-response-headers

Conversation

@adriangb
Copy link
Copy Markdown
Member

@adriangb adriangb commented May 5, 2026

Summary

Split out from #1905 (per review).

Gives the Logfire backend an out-of-band channel — independent of response bodies — to deprecate endpoints or signal hard failures to any client running this SDK.

A new logfire/_internal/server_response.py module installs a requests response hook on every Logfire-bound HTTP session (OTLP exporters, token validation, CRUD client, variables provider, CLI). Each Logfire response is checked for two custom headers:

Header Effect
X-Logfire-Warning: <message> warnings.warn(message, LogfireServerWarning). Python's default filter dedupes identical messages within a process, so a chatty server only warns once.
X-Logfire-Error: <message> Raises LogfireServerError. OTLP/variables paths already swallow exceptions from their HTTP calls; CRUD and CLI propagate the error to the user.

Both headers are optional — absent headers are silently ignored, so the existing wire format is unaffected for clients running an older SDK.

Test plan

  • New tests/test_server_response.py covers warning emission, dedup, error raising, and end-to-end propagation through LogfireClient.
  • make format lint typecheck clean.
  • uv run pytest tests/test_server_response.py tests/test_configure.py tests/test_client.py tests/test_variables.py tests/test_cli.py (451 tests) passes.

🤖 Generated with Claude Code

Adds a `requests` response hook that the SDK installs on every
Logfire-bound HTTP session (OTLP exporters, token validation, CRUD
client, variables provider, CLI). The hook reads two custom headers
the server attaches to responses:

* `X-Logfire-Warning` -> `warnings.warn(..., LogfireServerWarning)`.
  Python's default filter dedupes identical messages within a process,
  so a chatty server only warns once.
* `X-Logfire-Error` -> raises `LogfireServerError`. OTLP/variables
  paths already swallow exceptions from their HTTP calls; CRUD/CLI
  propagate the error to the user.

This gives the backend an out-of-band channel to deprecate endpoints
or signal hard failures without piggy-backing on response bodies.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@adriangb adriangb requested review from Viicos and alexmojaki May 5, 2026 14:24
@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages Bot commented May 5, 2026

Deploying logfire-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit: 4808b4f
Status: ✅  Deploy successful!
Preview URL: https://a0d97e8d.logfire-docs.pages.dev
Branch Preview URL: https://add-server-response-headers.logfire-docs.pages.dev

View logs

Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 7 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

adriangb added a commit that referenced this pull request May 5, 2026
Per review (#1905#discussion_r3189043209), the X-Logfire-Warning /
X-Logfire-Error response-hook handling is logically independent from
the X-Logfire-Telemetry request header — they just happened to be
introduced together. Moved that code (plus its exceptions, install
calls, and tests) to a separate PR (#1906) so each can be reviewed
and landed on its own.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
warnings.warn(warning_message, LogfireServerWarning, stacklevel=2)
error_message = response.headers.get(ERROR_HEADER_NAME)
if error_message:
raise LogfireServerError(error_message)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I worry that users might have a reason to opt out of this and won't be able to. Maybe that's paranoid.

We could theoretically make this hook configurable, defaulting to this one, but allowing users to take any action for all API responses. Probably overkill.

I'd like to know what concrete use cases we have in mind for both warnings and errors.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a way to opt out in 278a76c

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to know what concrete use cases we have in mind for both warnings and errors.

From the PR description:

to deprecate endpoints or signal hard failures to any client running this SDK

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we'd deprecate endpoints by changing code here though, right? or is the point to push users to upgrade their sdk?

hard failures like what? where would the server respond with the error header instead of a 4xx/5xx which would have a similar effect here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes the point is that we can propagate deprecation to old versions of the SDK.

hard failures like what?

The error part was a bit more just "seems like it could be useful". But I imagine it might be more informative to raise a LogfireError: Some explanation Than hope the 4xx bubbles up correctly in a way that is usable by users.

I also wonder if we could emit an error / warn logfire log so it shows up in users projects 🤔

Lets users intercept every Logfire API response (OTLP exports, credential
init, variables provider). Default keeps the existing X-Logfire-Warning /
X-Logfire-Error handling; pass `lambda response: None` to opt out, or
compose around `process_logfire_response_headers`.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 5 files (changes from recent commits).

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="logfire/_internal/config.py">

<violation number="1" location="logfire/_internal/config.py:1333">
P2: `_lazy_init_variable_provider` (line 1462) creates a `LogfireRemoteVariableProvider` without passing `transport_response_hook`, so lazily-initialized providers won't surface server warning/error headers. Add `transport_response_hook=self.advanced.transport_response_hook` there as well.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Comment thread logfire/_internal/config.py
adriangb and others added 2 commits May 6, 2026 09:31
The example references `logfire`/`my_metric` without imports, which
test_docs runs through pytest-examples and trips on.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
`_lazy_init_variable_provider` was constructing `LogfireRemoteVariableProvider`
without the configured hook, so providers spun up via the lazy path bypassed
the user-supplied response handling.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@adriangb adriangb requested a review from alexmojaki May 6, 2026 17:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants